package com.zhoug.tmap.http;

import com.zhoug.tmap.dianditu.Requests;

import retrofit2.Retrofit;

/**
 * @Author: zhoug
 * @Date: 2025-01-03
 * @Description:
 */
public class HttpClient {
    private static final String TAG = ">>>HttpClient";
    private static boolean debug = true;
    private Retrofit retrofit;
    private Requests baseRequests;
    private String baseUrl;

    public HttpClient() {

    }

    /**
     * 初始化
     */
    private void init() {
        HttpLogInterceptor logInterceptor = new HttpLogInterceptor();
        if (debug) {
            logInterceptor.setLevel(HttpLogInterceptor.Level.BODY);
        } else {
            logInterceptor.setLevel(HttpLogInterceptor.Level.NONE);
        }


        retrofit = new MyRetrofitBuilder()
                .setDebug(debug)
                .setDebugLevel(null)
                .setConnectTimeOut(30)
                .setReadTimeOut(120)
                .setWriteTimeOut(120)
                .addInterceptor(logInterceptor)
                .build(baseUrl);


    }

    /**
     * BaseRequests请求接口
     *
     * @return
     */
    public Requests getBaseRequests() {
        if (baseRequests == null) {
            synchronized (this) {
                if (baseRequests == null) {
                    baseRequests = getRequest(Requests.class);
                }
            }
        }
        return baseRequests;
    }

    /**
     * 创建请求接口
     *
     * @param cls
     * @param <T>
     * @return
     */
    public <T> T getRequest(Class<T> cls) {
        if (retrofit == null) {
            init();
        }
        return retrofit.create(cls);
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public static void setDebug(boolean debug) {
        HttpClient.debug = debug;
    }

    public static boolean isDebug() {
        return debug;
    }


}
